package com.scs.application.modules.wos.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.RestResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.wos.consts.TaskFlowStatus;
import com.scs.application.modules.wos.dto.WoTaskCreateDto;
import com.scs.application.modules.wos.entity.WosWoTask;
import com.scs.application.modules.wos.service.WosWoTaskService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  工单任务服务层
 */
@RestController
@RequestMapping("/wos/wo_task")
public class WosWoTaskController extends CrudController<WosWoTask,String,WosWoTaskService>{
    /**
     * @param id
     * @Description:  处理开始
     */
    @PostMapping("/{id}/handleStart")
    public CommonResult handleStart(@RequestBody WosWoTask task) {
        return CommonResult.ok("处理开始",service.handleStatus(task, TaskFlowStatus.handle_start,TaskFlowStatus.handle_doing));
    }

    /**
     * @Description:  处理完成
     */
    @PostMapping("/{id}/handleEnd")
    public CommonResult handleEnd(@RequestBody WosWoTask task) {
        return CommonResult.ok("处理完成",service.handleStatus(task, TaskFlowStatus.handle_doing,TaskFlowStatus.handle_end));
    }

    /***
     * @Description:  外部系统调用-SCP系统-推送-创建工单-单个
     */
    @PostMapping("/createOneForTpcItsm")
    public RestResult createOneForTpcItsm(@RequestBody WoTaskCreateDto taskCreateDto) {
        synchronized (taskCreateDto.getId().intern()) {
            try {
                service.createOneForTpcItsm(taskCreateDto);
            }catch (Exception e) {
                return  RestResult.error("工单创建失败,原因：" + e.getMessage());
            }
        }
        return RestResult.ok();
    }

    /***
     * @Description:  外部系统调用-SCP系统-推送-任务列表
     */
    @PostMapping("/getListForTpcItsm")
    public RestResult getListForTpcItsm(@RequestBody List<String> idList) {
        if (idList == null || idList.isEmpty()) {
            return RestResult.error("操作失败，缺少参数idList");
        }
        return RestResult.ok(
                "操作成功",
                    service.list(Wrappers.<WosWoTask>query()
                            .in("task_id_itsm",idList )
                            .ne("flow_status",TaskFlowStatus.handle_start )
                    )
        );
    }
}
